<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>景区预约管理系统</h2>
        <p>欢迎登录</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/auth'

const router = useRouter()

// 登录表单
const loginFormRef = ref()
const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}



const loading = ref(false)

// 登录处理
const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true

    // 模拟登录验证（暂时使用模拟数据，后续可替换为真实API调用）
    await new Promise(resolve => setTimeout(resolve, 1000)) // 模拟网络延迟
    
    // 模拟用户数据
    const mockUsers = [
      { username: 'admin', password: 'admin123', phone: '13800138000', email: 'admin@example.com', role: 'admin' },
      { username: 'user1', password: 'user1123', phone: '13900139000', email: 'user1@example.com', role: 'user' },
      { username: 'manager', password: 'manager123', phone: '13700137000', email: 'manager@example.com', role: 'manager' }
    ]

    const user = mockUsers.find(u => u.username === loginForm.username && u.password === loginForm.password)
    
    if (user) {
      // 存储token和用户信息
      const token = `mock-token-${Date.now()}`
      const userInfo = {
        id: 1,
        username: user.username,
        phone: user.phone,
        email: user.email,
        role: user.role,
        avatar: '',
        status: 'active',
        createTime: new Date().toISOString()
      }
      
      localStorage.setItem('token', token)
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      throw new Error('用户名或密码错误')
    }
  } catch (error) {
    console.error('登录失败', error)
    ElMessage.error(error.message || '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}


</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: url('@/assets/yfzz.jpg') center/cover no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  width: 400px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 8px;
  padding: 40px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  margin-bottom: 10px;
  font-size: 24px;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

:deep(.el-input__wrapper) {
  border-radius: 6px;
}

:deep(.el-button) {
  border-radius: 6px;
}
</style>